﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Veiculo.Modelo
{
    public partial class Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                try
                {

                    string idModelo = Request.QueryString["Id"].Trim();

                    eutolisodbEntities1 context = new eutolisodbEntities1();

                    int id_Modelo = Convert.ToInt32(idModelo);

                    var _query = (from mo in context.modelo
                                  join m in context.marca
                                  on mo.Marca equals m.Id
                                  where mo.Id == id_Modelo
                                  select new
                                  {
                                      mo.Id,
                                      mo.Nome,
                                      mo.Versao,
                                      mo.Observação,
                                      marca = m.Nome
                                      

                                  });


                    foreach (var Result in _query)
                    {
                        txtNome.Text = Result.Nome;
                        txtVersao.Text = Result.Versao;
                        txtObs.Text = Result.Observação;
       

                    }


                }
                catch (Exception)
                {


                }

            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            try
            {

                string idModelo = Request.QueryString["Id"].Trim();

                eutolisodbEntities1 context = new eutolisodbEntities1();

                int id_Modelo = Convert.ToInt32(idModelo);

                var _query = (from mo in context.modelo
                              join m in context.marca
                              on mo.Marca equals m.Id
                              where mo.Id == id_Modelo
                              select mo).First();


                _query.Nome = txtNome.Text;
                _query.Versao = txtVersao.Text;
                _query.Observação = txtObs.Text;
                _query.Marca = Convert.ToInt32( ddlMarca.SelectedValue);    

                context.SaveChanges();

                lblMsg.Visible = true;

            }


            catch (Exception)
            {
                lblMsg.Visible = true;
                lblMsg.Text = "Ocorreu um problema ao editar o item.";
            }

            Response.Redirect("Home.aspx");
        }
    }
}